package org.lmj.dim.controller;

import lombok.extern.slf4j.Slf4j;
import org.lmj.dim.dto.UserDTO;
import org.lmj.dim.mapper.UserMapper;
import org.lmj.dim.po.ServerResponse;
import org.lmj.dim.po.UserEntity;
import org.lmj.dim.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;


@RestController
@RequestMapping
@Slf4j
public class RegisterController {

    @Autowired
    UserMapper userMapper;

    @PostMapping("/regist")
    public ServerResponse register(@Valid @RequestBody UserDTO userDTO){
        log.info("注册参数为{}",userDTO);
        userDTO.setPassword(SecurityUtils.encodePassword(userDTO.getPassword()));
        UserEntity userEntity = new UserEntity();
        BeanUtils.copyProperties(userDTO, userEntity);
        int res = userMapper.insert(userEntity);
        if (res==1){
            return ServerResponse.success(res);
        }
       return ServerResponse.fail(400,"用户名重复");
    }
}
